//
//  ZJFThiredTableViewCell.m
//  OurTourism
//
//  Created by 张金峰 on 15/12/1.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
#import "ZJFThiredTableViewCell.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
@interface ZJFThiredTableViewCell ()
@property(nonatomic, strong)UIImageView *tupian;
@property(nonatomic, strong)UILabel *XiangQing;
@property(nonatomic, strong)UIImageView *biao;
@property(nonatomic, strong)UILabel *riQi;
@property(nonatomic, strong)UIView *beijingView;
@property(nonatomic, strong)UILabel *word;
@property(nonatomic, strong)UILabel *time;
@property(nonatomic, strong)UIImageView *colock;
@property(nonatomic, strong)UILabel *gray;
@property(nonatomic, assign)NSInteger imgH;
@end
@implementation ZJFThiredTableViewCell

- (void)awakeFromNib {
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView
{
    self.beijingView = [[UIView alloc] init];
    [self.contentView addSubview: self.beijingView];
    self.beijingView.backgroundColor = [UIColor whiteColor];
    
    self.tupian = [[UIImageView alloc] init];
    [self.beijingView addSubview: self.tupian];
    self.tupian.backgroundColor = [UIColor redColor];
    
    self.word = [[UILabel alloc] init];
    self.word.numberOfLines = 0;
    self.word.font = [UIFont systemFontOfSize:17];
    [self.beijingView addSubview:self.word];
    //self.word.backgroundColor = [UIColor yellowColor];
    
    self.time = [[UILabel alloc] init];
    [self.beijingView addSubview:self.time];
    //self.time.backgroundColor = [UIColor blueColor];
    
    self.colock = [[UIImageView alloc] init];
    [self.beijingView addSubview:self.colock];
    self.colock.backgroundColor = [UIColor redColor];
    
    self.gray = [[UILabel alloc] init];
    [self.beijingView addSubview:self.gray];
    self.gray.backgroundColor = [UIColor colorWithRed:225 / 255. green:225 / 255. blue:225 / 255. alpha:1];
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
    CGFloat h = [[self class]heightWithText:self.Model.words fontSize:17 Width:[UIScreen mainScreen].bounds.size.width - 40];
//    [self.beijingView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.contentView.mas_top).offset(10);
//        make.left.mas_equalTo(self.contentView.mas_left).offset(10);
//        //make.size.mas_equalTo(CGSizeMake(self.contentView.frame.size.width - 20, 255 + 5 + h + 2 + 20 + 15));
//        make.size.mas_equalTo(CGSizeMake(self.contentView.frame.size.width - 20, 500));
//    }];
    self.beijingView.frame = CGRectMake(10, 10, self.contentView.frame.size.width - 20, 255 + 5 + h + 2 + 20 + 15);
//
    [self.tupian mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.beijingView.mas_top).offset(0);
        make.left.mas_equalTo(self.beijingView.mas_left).offset(0);
        make.size.mas_equalTo(CGSizeMake(self.beijingView.frame.size.width, self.imgH));
    }];
    self.word.frame = CGRectMake(10, 265, [UIScreen mainScreen].bounds.size.width - 40, h);
//    [self.word mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.tupian.mas_bottom).offset(10);
//        make.left.mas_equalTo(self.beijingView.mas_left).offset(10);
//        make.right.mas_equalTo(self.beijingView).offset(-10);
//        make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, h));
//    }];
    
    [self.colock mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.word.mas_bottom).offset(10);
        make.left.mas_equalTo(self.beijingView.mas_left).offset(10);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.word.mas_bottom).offset(10);
        make.left.mas_equalTo(self.colock.mas_right).offset(2);
        make.size.mas_equalTo(CGSizeMake(200, 15));
    }];
    [self.gray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.beijingView.mas_bottom).offset(0);
        make.left.mas_equalTo(self.beijingView.mas_left).offset(0);
        make.size.mas_equalTo(CGSizeMake(self.beijingView.frame.size.width, 2));
    }];
}

- (void)setModel:(ZJFguiDeModel *)Model
{
    if (_Model != Model) {
        _Model = Model;
    }
//    NSString *str = [NSString stringWithFormat:@"%@%@%@",modelRecords.picdomain, @"upload/", modelRecords.picfile];
//    [self.img sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:nil];
    NSString *str = [NSString stringWithFormat:@"%@%@%@",Model.picdomain,@"upload/",Model.picfile];
  //  NSLog(@"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa                 %@",str);
    if (str == nil) {
        self.imgH = 0;
    }else{
        self.imgH = 255;
    }
    [self.tupian sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:nil];
    self.word.text = Model.words;
    self.time.text = Model.timestamp;
}

//计算文本高度的方法
+ (CGFloat)heightWithText:(NSString *)text fontSize:(CGFloat)fontsize Width:(CGFloat)width

{
    //设置一个字典 保存文本属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontsize]};
    
    //预设尺寸 文本最大不会超过这个范围
    CGSize size = CGSizeMake(width, 2000);
    //根据文本信息 计算得到一个CGRect
    //参数1. 尺寸范围
    //参数2. 获取文本尺寸的方式
    //参数3. 文本属性
    //参数4. 无用信息(上下文)
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end

